<?php
/**
 * DMFramework
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @url http://dmframework.nl
 */

/**
 * With the HTTP class you can get and post a new request to a certain server.
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.Library
 */
class Dmf_Http
{
    /**
     * The default manager
     * 
     * @var string
     */
    private $_requestManager = 'curl';
    
    /**
     * Manager object
     * 
     * @var mixed
     */
    private $_manager;
    
    /**
     * The cUrl library
     * 
     * @var int 
     */
    const CURL = 3;
    
    /**
     * The file_get_contents library
     * 
     * @var int 
     */
    const FILE_GET_CONTENTS = 4;
    
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->_manager = function_exists( 'curl_init' ) ? new Dmf_Http_Manager_Curl : new Dmf_Http_Manager_Fgc;
    }
    
    /**
     * Get request
     * 
     * @param string $url
     * @param array $options
     * @return Dmf_Http_Request 
     */
    public function get( $url, $options = array() )
    {
        return $this->_manager->get( $url, $options );
    }
    
    /**
     * Post request
     *  
     * @param string $url
     * @param array $data
     * @param array $options
     * @return Dmf_Http_Request
     */
    public function post( $url, $data, $options = array() )
    {
        return $this->_manager->post( $url, $data, $options );
    }
    
    /**
     * Define which Framework to use
     * 
     * @param string $manager 
     */
    public function useFramework( $manager )
    {
        $this->_requestManager = $manager;
        
        switch( $manager )
        {
            case Dmf_Http::CURL:
                $this->_manager = new Dmf_Http_Manager_Curl;
                break;
            case Dmf_Http::FILE_GET_CONTENTS:
                $this->_manager = new Dmf_Http_Manager_Fgc;
                break;
        }
    }
    
    /**
     * I know... it looks silly to have a function for a function,
     * but I keep forgetting that damn name!
     * 
     * @param string $domain
     * @param string $type
     * @return boolean 
     */
    public function isRecord( $domain, $type )
    {
        return checkdnsrr( $domain, $type );
    }
    
    /**
     * Checks if a port is open on an webserver
     * 
     * @param string $server
     * @param mixed $port 
     * @param boolean $getInfo
     */
    public function isOnline( $server, $port, $getInfo = false )
    {
        if ( ! is_numeric( $port ) )
        {
            $mode = 'tcp';
            
            if ( preg_match( '/udp/', $port ) )
            {
                $mode = 'udp';
                $port = substr( $port, 0, -3 );
            }
            
            if ( preg_match( '/tcp/', $port ) )
            {
                $port = substr( $port, 0, -3 );
            }
            
            $port = getservbyname( $port, $mode );
        }
        
        $socket_con = @fsockopen( $server, $port, $error_number, $error_string, 1 );
        
        if ( $getInfo === false )
        {
            return ( $socket_con === false ) ? false : true;
        }
        else
        {
            return array( 
                'result'        => ( $socket_con === false ) ? false : true,
                'error_number'  => $error_number,
                'error_string'  => $error_string,
                'server'        => $server,
                'port'          => $port
            );
        }
    }
}